package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.Order;
import com.markerhub.entity.Tick;
import com.markerhub.service.OrderService;
import com.markerhub.service.TickService;
import com.markerhub.util.ShiroUtil;
import com.markerhub.vo.OrderUserVO;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    TickService tickService;

    @GetMapping("/test")//旧版多表关联id
    public Result list(@RequestParam(defaultValue = "1") Integer currentpage){//currentpage 这个名字需要和vue匹配
        Page page = new Page(currentpage,2);//size:一页显示多少个
        IPage pageData= orderService.getOrderUser(page);//getOrderUser这个方法在OrderMapper中定义sql语法
        //具体用法参考OrderService定义方法类型
        //impl实例化
        System.out.println(currentpage);
        System.out.println(pageData.getSize());
        return  Result.succ(pageData);
    }


    //修改参考blog edit(与添加一致)\
    @RequiresAuthentication
    @PostMapping("/tick/buy")
    public Result buytick(@Validated @RequestBody Order order){
        System.out.println("-----------------------------------------");
        System.out.println(order);
        if(order.getTickId()==null){
            return Result.faile("购买失败");
        }
        Tick tick= tickService.getById(order.getTickId());
        System.out.println(tick);
        System.out.println(ShiroUtil.getProfile().getId().longValue());
        if(tick.getNum()-1<0){
            return Result.faile("数量不足,购买失败");
        }
        tick.setNum(tick.getNum()-1);
        tickService.saveOrUpdate(tick);
        order.setStatus(tick.getStatus()).setPrice(tick.getPrice()).setUserId((int) ShiroUtil.getProfile().getId().longValue()).setCreated(LocalDateTime.now());

        System.out.println("-----------------------------------------");
        System.out.println(order);
        orderService.saveOrUpdate(order);

        return  Result.succ("购买成功");
    }


    @GetMapping("/test2")//显示全部订单
    public Result getlistbyid(@RequestParam(defaultValue = "1") Integer currentpage){//currentpage 这个名字需要和vue匹配
        Page page = new Page(currentpage,20);//size:一页显示多少个
        //IPage pageData= orderService.getOrderUser(page);//getOrderUser这个方法在OrderMapper中定义sql语法
        IPage pageDate = orderService.page(page,new QueryWrapper<Order>());
        //具体用法参考OrderService定义方法类型
        //impl实例化
        System.out.println(currentpage);
        System.out.println(pageDate.getSize());
        return  Result.succ(pageDate);
    }

    @GetMapping("/test3")//显示当前用户id //个人信息查看我的订单用这个 //未解决是否购买功能
    public Result getlistbyuserId(@RequestParam(defaultValue = "1") Integer currentpage){//currentpage 这个名字需要和vue匹配
        Page page = new Page(currentpage,20);//size:一页显示多少个
        //IPage pageData= orderService.getOrderUser(page);//getOrderUser这个方法在OrderMapper中定义sql语法
        IPage pageDate = orderService.page(page,new QueryWrapper<Order>().eq("user_id",1));//具体userId获取参考下面
        //具体用法参考OrderService定义方法类型
        //impl实例化
        System.out.println(currentpage);
        System.out.println(pageDate.getSize());
        return  Result.succ(pageDate);
    }



    //需要认证权限才能查询订单
    @RequiresAuthentication
    @GetMapping("/list")
    public Result userorder(@RequestParam(defaultValue = "1") Integer currentpage){//currentpage 这个名字需要和vue匹配
        Page page = new Page(currentpage,2);//size:一页显示多少个
        IPage pageData= orderService.page(page,new QueryWrapper<Order>().eq("user_id", ShiroUtil.getProfile().getId().longValue()));//
        //具体用法参考OrderService定义方法类型
        //impl实例化
        System.out.println(currentpage);
        System.out.println(pageData.getSize());
        return  Result.succ(pageData);
    }



    @GetMapping("/user")
    public Result list2(@RequestParam(defaultValue = "1") Integer currentpage){//currentpage 这个名字需要和vue匹配
        Page page = new Page(currentpage,3);//size:一页显示多少个
        int testid= (int) ShiroUtil.getProfile().getId().longValue();
        IPage pageData= orderService.getAddressOrder(page,testid);//getOrderUser这个方法在OrderMapper中定义sql语法
        //具体用法参考OrderService定义方法类型
        //impl实例化
        System.out.println(currentpage);
        System.out.println(pageData.getSize());
        return  Result.succ(pageData);
    }

    @RequiresAuthentication
    @GetMapping("/myorder/{orderid}")
    public Result myorder(@PathVariable(name="orderid") int orderid){
        System.out.println("测试吧");
        //在sql上需要重新定制
        Order order=orderService.getById(orderid);
        Assert.isTrue(order.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(),"没有权限编辑");
        Assert.notNull(order,"无商品信息");
        int tickid=order.getTickId();
        System.out.println(tickid);
        Tick tick=tickService.getById(tickid);
        OrderUserVO orderUserVO=new OrderUserVO();
        //BeanUtil.copyProperties(order,orderUserVO,"id","userId","created","status","price","useTime","userReale","userPhone","userIdcard","qccode","tickId");
        //orderUserVO.set
        orderUserVO.setId(order.getId()).setUserId(order.getUserId()).setPrice(order.getPrice()).setCreated(order.getCreated()).setQccode(order.getQccode());
        orderUserVO.setStatus(order.getStatus()).setTickId(order.getTickId()).setUseTime(order.getUseTime()).setUserIdcard(order.getUserIdcard()).setUserPhone(order.getUserPhone());
        orderUserVO.setUserReale(order.getUserReale());
        orderUserVO.setTitle(tick.getTitle()).setDescription(tick.getDescription()).setContet(tick.getContet()).setPicture(tick.getPicture()).setSwiper(tick.getSwiper());
        orderUserVO.setSwipera(tick.getSwipera()).setSwiperb(tick.getSwiperb()).setType(tick.getType()).setStarttime(tick.getStarttime());
        orderUserVO.setUserAddress(order.getUserAddress()).setGoodsstatus(order.getGoodsstatus());
        return  Result.succ(orderUserVO);
    }

    @RequiresAuthentication
    @PostMapping("/refund" )
    public Result refundOrder(@Validated @RequestBody Order order){
        Assert.isTrue(order.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(),"没有申请权限");
        int Ordertype=order.getStatus();
        if(Ordertype==1){
            order.setStatus(3);
        }else if(Ordertype==2){
            order.setStatus(5);
        }

        orderService.saveOrUpdate(order);
        return  Result.succ("修改完成");
    }


    @RequiresAuthentication
    @PostMapping("/refund/{orderid}" )
    public Result refundOrderno(@Validated @RequestBody Order order,@PathVariable(name="orderid") int orderid){
        //Assert.isTrue(order.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(),"没有申请权限");
        int Ordertype=order.getStatus();
        System.out.println("orderid："+orderid);
        if(orderid==1){
            if(Ordertype==3){
                Assert.isTrue(1 == ShiroUtil.getProfile().getId().longValue(),"没有查看权限");
                order.setStatus(4);
            }else if(Ordertype==5){
                Assert.isTrue(1 == ShiroUtil.getProfile().getId().longValue(),"没有查看权限");
                order.setStatus(6);
            }
        }else if(orderid==0){
            if(Ordertype==3){
                Assert.isTrue(1 == ShiroUtil.getProfile().getId().longValue(),"没有查看权限");
                order.setStatus(1);
            }else if(Ordertype==5){
                Assert.isTrue(1 == ShiroUtil.getProfile().getId().longValue(),"没有查看权限");
                order.setStatus(2);
            }
        }


        orderService.saveOrUpdate(order);
        return  Result.succ("修改完成");
    }

    @RequiresAuthentication
    @GetMapping("/allord")//管理员查看全部订单
    public Result getallord(@RequestParam(defaultValue = "1") Integer currentpage){//currentpage 这个名字需要和vue匹配
        Page page = new Page(currentpage,20);//size:一页显示多少个
        //IPage pageData= orderService.getOrderUser(page);//getOrderUser这个方法在OrderMapper中定义sql语法
        Assert.isTrue(1 == ShiroUtil.getProfile().getId().longValue(),"没有查看权限");
        IPage pageData= orderService.getAllOrd(page);
        //具体用法参考OrderService定义方法类型
        //impl实例化
        System.out.println(currentpage);
        System.out.println(pageData.getSize());
        return  Result.succ(pageData);
    }




}
